package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/13 19:26
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper dtsRoleMapper;
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public Set<String> findRoleById(Integer[] ids) {

        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DtsRole::getId,ids);
        wrapper.eq(DtsRole::getDeleted,false);
        List<DtsRole> roleList = dtsRoleMapper.selectList(wrapper);
        Set<String> roles = roleList.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());
        return roles;
    }

    @Override
    public List<DtsRole> findAll() {
        List<DtsRole> roleList = dtsRoleMapper.selectList(null);
        return roleList;
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole> ipage = new Page<>(page,limit);
        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)){
            wrapper.like("name", rolename);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        dtsRoleMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return data;
    }

    @Override
    public void insert(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);

        dtsRoleMapper.insert(role);
    }

    @Override
    public void update(DtsRole role) {
        role.setUpdateTime(new Date());
        dtsRoleMapper.updateById(role);
    }


    @Override
    public void delete(DtsRole role) {
//        判断是否被管理员关联
        LambdaQueryWrapper<DtsAdmin> adminWrapper = new LambdaQueryWrapper<>();
        adminWrapper.select(DtsAdmin::getRoleIds);
        adminWrapper.eq(DtsAdmin::getDeleted,false);
        List<DtsAdmin> adminList = adminMapper.selectList(adminWrapper);
        for (DtsAdmin admin : adminList) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                if (roleId.equals(role.getId())){
                    throw new RuntimeException("该角色被管理员关联，不能删除");
                }
            }

        }
//        判断是否被权限关联
        LambdaQueryWrapper<DtsPermission> permissionWrapper = new LambdaQueryWrapper<>();
        permissionWrapper.eq(DtsPermission::getRoleId,role.getId());
        permissionWrapper.eq(DtsPermission::getDeleted,false);
        Integer count = permissionMapper.selectCount(permissionWrapper);
        System.out.println(count);
        if (count > 0 ){
            throw new RuntimeException("该角色被权限表关联，不能删除");
        }

        role.setDeleted(true);
        dtsRoleMapper.updateById(role);

    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole role = dtsRoleMapper.selectById(roleId);
        if (role.getName().equalsIgnoreCase("超级管理员")){
            return true;
        }
        return false;
    }


}
